﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using LGSVR.Common;
using LGSVR.BLL.Program;

namespace LGSVR_WEB.program
{
    public partial class messageInfoMain : LGSVR.Common.DataBasePage
   
    {
        string strcardDesc;
        string strWhere = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }

            BindmsgGroup();

            if (!IsPostBack)
            {

                  //if (!string.IsNullOrEmpty(Request["ID"]))
                  //    strWhere = "chkId = '" + Request["ID"] + "'";
                  if (!string.IsNullOrEmpty(Request["cardID"]) && Request["cardID"] !="0")
                  {
                      this.hdcardID.Value = Request["cardID"];
                      int totalRS;
                      DataTable dst = GetList("cardinfo", " cardType desc ", " AND cardID = '" + Request["cardID"] + "'", 1, 10, out totalRS);
                      strcardDesc = dst.Rows[0]["cardDesc"].ToString();
                      ////lblmessageCard.Text = "符合条件的记录，共有" + totalRS .ToString() + "条。当前所选信息卡：" + dst.Rows[0]["cardDesc"].ToString() + "。";  
                      ////该节目信息要属于传递过来的卡号
                      //strWhere = " and messageID in (select messageID from cardinfo where cardID = '" + this.hdcardID.Value + "')";
                      strWhere += " and cardID='" + Request["cardID"] +"'";

                      //绑定权限，显示自己有权限的卡组  中 起止节目编号 
                      if (Session["userID"] != null)
                      {
                          MessageInfo msgInfo = new MessageInfo();
                          int _beginActID = msgInfo.GetintValue("SELECT beginActID  FROM userCardGroup A,cardinfo B WHERE A.state ='1' AND userID = '" + Session["userID"] + "' AND A.CardGroupID = B.GroupID AND B.cardID = '" + Request["cardID"] + "'");
                          int _endActID = msgInfo.GetintValue("SELECT endActID  FROM userCardGroup A,cardinfo B WHERE A.state ='1' AND userID = '" + Session["userID"] + "' AND A.CardGroupID = B.GroupID AND B.cardID = '" + Request["cardID"] + "'");
                          strWhere += " AND actID between " + _beginActID + " AND " + _endActID;
                      }
                       
                      gvDataBind(strWhere);
                  }
                  else
                  {
                      lblmessageCard.Text = "<span style=\"color: #FF0000\">* 当前未选择信息卡，请点击左边信息卡树进行选择。</span>";                   

                  }


            }
           
        }

        private void BindmsgGroup()
        {
            BinddllList(ddlgroupID, "messageGroup", "GroupName", "messageGroupID", "", "ID");  //节目组下拉绑定


            //只有管理员和超级管理员才能看到即时信息
            if (Session["actID"] != null)
            {
                MessageInfo msgInfo = new MessageInfo();
                //只能通过角色名称判断，因为ID不确定
                string _ActName = msgInfo.GetstrValue("SELECT ActName  FROM userAct WHERE actID = " + Session["actID"]);
                if ((!_ActName.Contains("管理员")) && ddlgroupID.Items.Contains(ddlgroupID.Items.FindByText("即时信息")))
                {
                    ddlgroupID.Items.Remove(ddlgroupID.Items.FindByText("即时信息")); //按显示文本删除     
                }
            }
        }
        private void gvDataBind(string strWhere)
        {
            DataTable dt;
            int totalRS;
            int pageIndex;
            if (!string.IsNullOrEmpty(Request.QueryString["pg"]) && Utils.IsNumber(Request.QueryString["pg"]))
            {
                pageIndex = int.Parse(Request.QueryString["pg"]);
            }
            else
            {
                pageIndex = 1;
            }

            dt = GetList("VmessageInfo", " actID desc ", strWhere, pageIndex, PageSize, out totalRS);
            rptBody.DataSource = dt;
            rptBody.DataBind();
            SplitHtmlCode = Split.GetHtmlCode("messageInfolist.aspx", 8, pageIndex, PageSize, totalRS);

            lblmessageCard.Text = "符合条件的记录，共有" + totalRS.ToString() + "条。当前所选信息卡：" + strcardDesc + ""; 
        }
       

        protected void Search(object sender, CommandEventArgs e)
        {
            //string strWhere = "";
            if (ddlgroupID.SelectedIndex>0)
            {
                strWhere += " and groupID = '" + ddlgroupID.SelectedValue + "'";
            }
            if (txtactID.Text.Trim() != "")
            {
                strWhere += " and actID like '%" + txtactID.Text.Trim() + "%'";
            }
            if (txtmessageTxt.Text.Trim() != "")
            {
                strWhere += " and messageTxt like '%" + txtmessageTxt.Text.Trim() + "%'";
            }
            gvDataBind(strWhere);

        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            //string ss = Request["chkId"];
            bool isTrue = Delete("messageInfo", Request["chkId"]);
            if (isTrue)
            {
                MsgContent = ShowMessage("恭喜您，删除成功！", MessageType.Success, false, true);
                NoRefresh();
            }
            else
            {
                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            }
        }

        protected void lbtnUnable_Click(object sender, EventArgs e)
        {
            bool isTrue = UpdateState("messageInfo", false, Request["chkId"]);
            if (isTrue)
            {
                MsgContent = ShowMessage("恭喜您，停用成功！", MessageType.Success, false, true);
                NoRefresh();
            }
            else
            {
                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            }
        }

        protected void lbtaudit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request["chkId"]) || Request["chkId"].Contains(","))
            {
                MsgContent = ShowMessage("请选择一条节目信息进行审批。", MessageType.Error, false, true);
                return;
            }
           
            //int totalRS;
            //DataTable dt = GetList("messageInfo", " ID desc ", " AND ID=" + Request["chkId"], 1, 10, out totalRS);
            //string strmessageID = dt.Rows[0]["messageID"].ToString();

            //MessageInfo msgInfo = new MessageInfo();           
            ////先查询状态，是否审批
            //string _State = msgInfo.GetstrValue("SELECT State  FROM messageCard WHERE messageID = " + strmessageID);
            //if (_State == "W")
            //{
            //    MsgContent = ShowMessage("已经审批过的节目信息不能再进行审批。", MessageType.Error, false, true);
            //    return;
            //}

            int totalRS;
            DataTable dt = GetList("VmessageInfo", " ID desc ", " AND ID=" + Request["chkId"], 1, 10, out totalRS);
            string strmessageID = dt.Rows[0]["messageID"].ToString();
            string strmsgState = dt.Rows[0]["msgState"].ToString();
            string strgroupName = dt.Rows[0]["groupName"].ToString();

            if (strmsgState != "N")
            {
                MsgContent = ShowMessage("已经审批过(或删除)的节目信息不能再进行审批。", MessageType.Error, false, true);
                return;
            }
            else if (strgroupName == "即时信息")
            {
                MsgContent = ShowMessage("即时信息不需要审核。", MessageType.Error, false, true);
                return;
            }

            Dictionary<string, string> dicParam;
            dicParam = new Dictionary<string, string>();

            dicParam.Add("userSure", Session["UserID"].ToString());
            dicParam.Add("SureTime", DateTime.Now.ToShortDateString());
            dicParam.Add("State", "W");
            Edit("messageCard", dicParam, "messageID='" + strmessageID +"'");

            if (Result > 0)
            {
                MsgContent = ShowMessage("恭喜您，审核成功！", MessageType.Success, false, true);
                NoRefresh();
            }
            else
            {
                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            }
        }

        protected void lbtMessageTimes_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request["chkId"]) || Request["chkId"].Contains(","))
            {
                MsgContent = ShowMessage("请选择一条节目信息设置信息时间。", MessageType.Error, false, true);
                return;
            }
            MessageInfo msgInfo = new MessageInfo();           
           
            int _messageID = msgInfo.GetintValue("SELECT messageID  FROM messageInfo WHERE ID = " + Request["chkId"]);
            //先查询状态，是否审批
            string _State = msgInfo.GetstrValue("SELECT State  FROM messageCard WHERE messageID = " + _messageID);
            if (_State =="W")
            {
                MsgContent = ShowMessage("已经审批过的节目信息不能再设置信息时间。", MessageType.Error, false, true);
                return;
            }
            Page.ClientScript.RegisterStartupScript(typeof(string), "key1", "<script>window.open('MessageTimes.aspx?ID=" + _messageID + "&keepThis=true&TB_iframe=true&height=200&width=500','_parent')</script>"); 


        }


        private void BinddllList(DropDownList ddlList, string strTableName, string strText, string strValue, string strWhere, string strSort)
        {
            DataTable dt;
            int totalRS;

            dt = GetList(strTableName, strSort + " desc ", strWhere, 1, int.MaxValue, out totalRS);
            ddlList.DataSource = dt;
            ddlList.DataTextField = strText;
            ddlList.DataValueField = strValue;
            ddlList.DataBind();

            ddlList.Items.Insert(0, new ListItem("-请选择-", ""));
        }
        
    }
}